﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Negoci = DeltaFlex.Negoci;

namespace Vista.Controls
{
    public partial class AltaArticle : System.Web.UI.UserControl
    {
        #region Variables Globals
        public string Referencia
        {
            get
            {
                return (Convert.ToString(ViewState["_referencia"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_referencia"]));
            }
            set { ViewState["_referencia"] = value; }
        }

        public string NomArticle
        {
            get
            {
                return (Convert.ToString(ViewState["_nomArticle"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_nomArticle"]));
            }
            set { ViewState["_nomArticle"] = value; }
        }

        public string Unitats
        {
            get
            {
                return (Convert.ToString(ViewState["_unitats"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_unitats"]));
            }
            set { ViewState["_unitats"] = value; }
        }

        public decimal PVP
        {
            get
            {
                return (Convert.ToDecimal(ViewState["_pvp"]) == 0 ?
                   0 : Convert.ToDecimal(ViewState["_pvp"]));
            }
            set { ViewState["_pvp"] = value; }
        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region Carrega a memoria
        /// <summary>
        /// Carrega el contingut dels text Box del control Alta Client
        /// </summary>
        private void CarregarMemoria()
        {
            Referencia = txtReferencia.Text;
            NomArticle = txtNom.Text;
            Unitats = txtUnitats.Text;
            PVP = txtpvp.Text == string.Empty ? 0 : Convert.ToDecimal(txtpvp.Text);
        }
        #endregion

        #region Neteja Controls
        /// <summary>
        ///Neteja tots els controls del formulari.
        /// </summary>
        /// <param name="controles"></param>
        public void NetejaControls(ControlCollection controles)
        {
            foreach (Control control in controles)
            {
                if (control is TextBox)
                    ((TextBox)control).Text = string.Empty;
                else if (control is DropDownList)
                    ((DropDownList)control).ClearSelection();
                else if (control is RadioButtonList)
                    ((RadioButtonList)control).ClearSelection();
                else if (control is CheckBoxList)
                    ((CheckBoxList)control).ClearSelection();
                else if (control is RadioButton)
                    ((RadioButton)control).Checked = false;
                else if (control is CheckBox)
                    ((CheckBox)control).Checked = false;
                else if (control.HasControls())
                    //Esta linea detecta un Control que contigui altres Controls
                    //Així cap control es quedarà sense netejar
                    NetejaControls(control.Controls);
            }
        }
        #endregion

        #region Guardar
        protected void btnAltaArticle_Click(object sender, EventArgs e)
        {
            string missatge, exepcio;
            missatge = exepcio = string.Empty;
            CarregarMemoria();
            try
            {
                Negoci.Articles.InsertarArticle(Referencia, NomArticle, Unitats, PVP);
            }
            catch (Exception ex)
            {
                exepcio = "Error en donar d'alta un nou article <br/> " + ex.Message;
            }

            if (String.IsNullOrEmpty(exepcio))
                missatge = ConstantsAltes.mplbGuardarArticleOK;
            else
                missatge = ConstantsAltes.mplbGuardarArticleKO + " <br/> " + exepcio;

            mpAltaArticle.LabelHeader = ConstantsAltes.mplhGuardarArticle;
            mpAltaArticle.LabelBody = missatge;
            mpAltaArticle.MostraInfo = true;

            NetejaControls(this.Controls);

            mpAltaArticle.MostrarModal();
        }
        #endregion
    }
}